package com.mall.ums.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.model.domain.dto.UpdatePasswordParamDto;
import com.mall.model.domain.ums.Admin;
import com.mall.model.domain.ums.AdminRoleRelation;
import com.mall.model.domain.ums.Role;
import com.mall.model.domain.vo.AdminInfoVo;
import com.mall.ums.interceptor.AdminHolder;
import com.mall.ums.service.AdminRoleRelationService;
import com.mall.ums.service.AdminService;
import com.mall.ums.service.RoleService;
import com.mall.utils.PageResult;
import com.mall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminRoleRelationService adminRoleRelationService;

    @Autowired
    private RoleService roleService;

    /**
     * 删除指定用户信息
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R<String> delete(@PathVariable Integer id) {
        if (adminService.removeUser(id)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public R<AdminInfoVo> info() {
        Long id = AdminHolder.getAdminId();
        AdminInfoVo vo = adminService.getUserInfo(id);
        return R.success(vo);
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R<PageResult> list(String keyword, Integer pageNum, Integer pageSize) {
        Page<Admin> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (keyword == null) {
            lambdaQueryWrapper.orderByDesc(Admin::getCreateTime);
            adminService.page(page, lambdaQueryWrapper);
            PageResult pageResult = PageResult.init(page);
            return R.success(pageResult);
        }
        lambdaQueryWrapper.like(Admin::getUsername, keyword);
        lambdaQueryWrapper.orderByDesc(Admin::getCreateTime);
        adminService.page(page, lambdaQueryWrapper);
        PageResult pageResult = PageResult.init(page);
        return R.success(pageResult);
    }

    /**
     * 登录以后返回token
     *
     * @param map
     * @return
     */
    @PostMapping("/login")
    public R<Map> login(@RequestBody Map map, HttpServletRequest request) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        Map retMap = adminService.loginVerification(username, password, request);
        return R.success(retMap);
    }

    /**
     * 登出功能
     *
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout() {
        AdminHolder.remove();
        return R.success("退出成功");
    }

    /**
     * 刷新token
     *
     * @param request
     * @return
     */
    @GetMapping("/refreshToken")
    public R refreshToken(HttpServletRequest request) {
        Admin admin = AdminHolder.get();
        Map map = new HashMap<>();
        map.put("username", admin.getUsername());
        map.put("password", admin.getPassword());
        AdminHolder.remove();
        R<Map> retMap = login(map, request);
        return R.success(retMap);
    }

    /**
     * 用户注册
     *
     * @param admin
     * @return
     */
    @PostMapping("/register")
    public R<Admin> register(@RequestBody Admin admin) {
        Admin retAdmin = adminService.register(admin);
        return R.success(retAdmin);
    }

    /**
     * 给用户分配角色
     *
     * @param map
     * @return
     */
    @PostMapping("/role/update")
    public R<String> roleUpdate(@RequestBody Map map) {
        Integer adminId = (Integer) map.get("adminId");
        ArrayList<Integer> roleIds = (ArrayList<Integer>) map.get("roleIds");
        adminRoleRelationService.roleUpdate(adminId, roleIds);
        return R.success("分配成功");
    }

    /**
     * 获取指定用户的角色
     *
     * @param adminId
     * @return
     */
    @GetMapping("/role/{adminId}")
    public R<List<Role>> getRoleByAdminId(@PathVariable Integer adminId) {
        List<Role> roles = roleService.getRoleByAdminId(adminId);
        return R.success(roles);
    }

    /**
     * 修改指定用户信息
     *
     * @param admin
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public R<String> updateById(@RequestBody Admin admin, @PathVariable Integer id) {
        boolean b = adminService.updateAdminById(admin, id);
        if (b) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 修改指定用户密码
     *
     * @param updatePasswordParamDto
     * @return
     */
    @PostMapping("/updatePassword")
    public R<String> updatePassword(@RequestBody UpdatePasswordParamDto updatePasswordParamDto) {
        adminService.updatePassword(updatePasswordParamDto);
        return R.success("修改密码成功");
    }

    /**
     * 修改帐号状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public R<String> updateStatus(@PathVariable Integer id, Integer status) {
        adminService.updateStatus(id, status);
        return R.success("修改状态成功");
    }

    /**
     * 获取指定用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Admin> getById(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        return R.success(admin);
    }
}
